using System;
using UnityEngine;

namespace Framework.Event
{
    /// <summary>事件优先级枚举</summary>
    public enum EnEventPriority
    {
        /// <summary>高</summary>
        High,
        /// <summary>中等</summary>
        Medium,
        /// <summary>低</summary>
        Low,
    }

    public class EventUtil : IDisposable
    {
        private string mEventKey = string.Empty;
        private EventHandler mEventHandler = null;
        private EnEventPriority mPriority = EnEventPriority.Medium;
        private EventArgs mEventArgsCache = null;

        public string EventKey => mEventKey;
        public EventHandler EventHandler => mEventHandler;
        public EnEventPriority Priority => mPriority;
        public EventArgs EventArgsCache => mEventArgsCache;

        public EventUtil(string pEventKey, EventHandler pEventHandler, EnEventPriority pPriority)
        {
            mEventKey = pEventKey;
            mEventHandler = pEventHandler;
            mPriority = pPriority;
        }

        /// <summary>设置参数</summary>
        /// <param name="pEventArgs"></param>
        public void SetEventArgs(EventArgs pEventArgs)
        {
            mEventArgsCache = pEventArgs;
        }

        /// <summary>发送事件</summary>
        public void Send()
        {
            if (mEventHandler != null)
                mEventHandler.Invoke(mEventKey, EventArgsCache);
            else
                Debug.LogWarning($"Send event failed, mEventHandler is null. mEventKey is {mEventKey}");
        }

        public override bool Equals(object obj)
        {
            return obj is EventUtil other && this == other;
        }

        public static bool operator ==(EventUtil left, EventUtil right)
        {
            if (left is null)
                return right is null;
            if (right is null)
                return false;

            return left.mEventKey == right.mEventKey && left.mEventHandler == right.mEventHandler;
        }

        public static bool operator !=(EventUtil left, EventUtil right)
        {
            return !(left == right);
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(mEventKey, mEventHandler);
        }

        public void Dispose()
        {
            mEventHandler = null;
            mEventArgsCache = null;
        }
    }
}